"""
新建项目
"""
import pytest
from Config.Base import Base
from playwright.sync_api import Page, expect, Playwright
from Config.config import *
from Config.URL import *
import allure, os
from Config.Base import Base
from Config.Tool import tool

# 测试用例数据
case_data = {'module':'用户管理','caseid':'新建成功','account':'zhangsan','username':'张三'}

@pytest.mark.基础管理系统
@pytest.mark.项目管理
@allure.step(case_data['caseid'])
def test_case(playwright : Playwright) -> None:
    # 测试结果标题
    allure.dynamic.feature(case_data['module'])
    allure.dynamic.title(case_data['caseid'])
    allure.dynamic.suite(case_data['caseid'])

    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page
    page.goto(url+url_login)
    page.get_by_placeholder("请输入用户名").fill(username_admin)
    page.get_by_placeholder("请输入密码").fill(password_admin)
    page.get_by_role("button", name="登 录").click()
    page.get_by_text("UAT-环北部湾广西水资源配置工程").first.click()
    page.wait_for_timeout(1000)
    page.goto(url+url_xiangmuliebiao)
    page.get_by_role("button", name=" 新建").click()
    page.locator("form").filter(has_text="项目名称 0/200 项目编码 0/200 项目中文简称").get_by_placeholder(
        "请输入项目名称").fill("AutoTest-项目1")
    page.get_by_placeholder("请输入项目编码").fill("ZC-XM1")
    page.locator(".vue-treeselect__input").first.click()
    page.get_by_text("环北部湾广西水资源配置有限公司").click()
    page.get_by_placeholder("请选择项目类型").click()
    page.locator("li").filter(has_text="公路").click()

    with page.expect_file_chooser() as fc_info:
        page.locator(".el-upload > .el-icon-plus").click()
    file_chooser = fc_info.value
    file_chooser.set_files(test_datas_dir+"\图片.jpg")
    allure.step("等待上传完成")

    # page.locator(".el-upload > .el-icon-plus").click()
    # page.locator(".el-upload").first.set_input_files("羊驼-小.jpg")

    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("paragraph")).to_contain_text("新建成功！")


    # 测试结果截图
    filename = os.path.join(test_screenshot_dir, f"{case_data['caseid']}.png")
    page.screenshot(path=filename)
    allure.attach.file(source=filename, name=case_data['caseid'],
                       attachment_type=allure.attachment_type.PNG)

    page.get_by_role("textbox", name="请输入项目名称").first.fill("AutoTest-项目1")
    page.get_by_role("button", name=" 查询").click()
    # 获取tbody下面元素tr个数
    expect(page.locator("tbody > tr")).to_have_count(1)
    # 获取是否存在文本text
    expect(page.locator("tbody")).to_contain_text("AutoTest-项目1")
    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_text('删除成功！')).to_be_visible()




    page.wait_for_timeout(2000)
    page.goto(url+url_xiangmuhuishouzhan)
    page.get_by_role("textbox", name="请输入项目名称").first.fill("AutoTest-项目1")
    page.get_by_role("button", name=" 查询").click()
    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确定").click()
    expect(page.get_by_role("alert")).to_contain_text("操作成功！")

    SetupTeardown.close()
